<?php
/**
 * 后台登陆
 */
namespace app\admin\controller;
use think\Controller;
use think\Loader;
use think\Request;
use think\Db;
class Shop extends Controller
{
    // 登陆
    public function index(){
        return $this->fetch();
    }
    public function add(){
        $request = Request::instance();
        if($request->isAjax()){
            $data = [       //接受传递的参数
                'shop_title' => input('shop_title'),
                'shop_desc' => input('shop_desc'),
                'shop_img' => input('shop_img'),
            ];
            if(DB::name('shop') -> insert($data)){  
                 //添加数据
                 return json(array('code' => 200, 'msg' => '添加成功'));
                 return $this->success('添加成功');    //成功后跳转  lst 界面
                 
            }else{
                 return json(array('code' => 0, 'msg' => '添加失败'));
                 return $this->error('添加失败');
            }
            return;
        }
        return $this->fetch();  
    }
    public function shoplist()
    {
        $page = input('get.page');
        $pagesize = input('get.limit');
        $start = $pagesize*($page-1);
        $end = $pagesize*$page;
        $list = DB::name('shop');
        $res["data"]=$list->limit($start.','.$pagesize)->select();
        $res["code"]=0;
        $res["msg"]="成功";
        $res["count"]=$list->count();
        // var_dump($res);
        return $res;
    }
    public function infolist()
    {   
        $shop_value = input('get.shop_value');
        $page = input('get.page');
        $pagesize = input('get.limit');
        $start = $pagesize*($page-1);
        $end = $pagesize*$page;
        $list = DB::name('shop_spc');
        $res["data"]=$list->where('shop_id='.$shop_value)->limit($start.','.$pagesize)->select();
        $res["code"]=0;
        $res["msg"]="成功";
        $res["count"]=$list->where('shop_id='.$shop_value)->count();
        // var_dump($res);
        return $res;
    }
    public function del()
    {
        $id = input('post.id');
        $del = model('Shop')->delId($id);
        if ($del) {
            return 1;
        } else {
            return 2;
        }
    }
    public function info_add()
    {
        $shopId = input('get.shop_id');
        $this->assign('shopId',$shopId);
        $request = Request::instance();
        if($request->isAjax()){
            $data = [       //接受传递的参数
                'shop_id' => input('shop_id'),
                'shop_spc_name' => input('shop_spc_name'),
                'shop_price' => input('shop_price'),
                'shop_stock' => input('shop_stock'),
                'shop_weight' => input('shop_weight'),
            ];
            if(DB::name('shop_spc') -> insert($data)){  
                 //添加数据
                 return json(array('code' => 200, 'msg' => '添加成功'));
                 return $this->success('添加成功');    //成功后跳转  lst 界面
                 
            }else{
                 return json(array('code' => 0, 'msg' => '添加失败'));
                 return $this->error('添加失败');
            }
            return;
        }
        return $this->fetch();  
    }
    //编辑页面
    public function edit(){
        //判断页面是否提交
        
        $request = Request::instance();
        if($request->isAjax()){
             $info = input('post.');
             $updateuser = model('shop')->updateUser($info);
            var_dump($info);
        /*  Db('表名') 数据库助手函数*/
            if($updateuser){
                 return 1;
            } else {
                return 2;
            }
            return;
        }else{
            $id = input('param.id');
            $list = DB::name('shop')->where('id='.$id)->find();
            $this->assign('list', $list);
            return $this->fetch();  
        }
    }
    public function shopDel($id)
    {
        $id = input('post.id');
        $del = model('Shop')->shopDel($id);
        if ($del) {
            return 1;
        } else {
            return 2;
        }
    }
    public function info()
    {
         //判断页面是否提交
         $shop_value = input('param.shop_value');
         var_dump($shop_value);
         $this->assign('shop_value', $shop_value);
        //  $request = Request::instance();
        //  if($request->isAjax()){
        //       $info = input('post.');
        //       $data = model('Shop')->data($info);
        //      if($data){
        //           return 1;
        //      } else {
        //          return 2;
        //      }
        //      return;
        //  }
         return $this->fetch();  
    }
}